
#include "startup_animation.h"
#include "pt_fs.h"
#include "pt_dev.h"
#include "sys_font.h"
#include "download_app.h"

char* midea_logo_img[] = {
    "MideaLogo/MideaLogo_00.png", "MideaLogo/MideaLogo_03.png", "MideaLogo/MideaLogo_07.png", "MideaLogo/MideaLogo_10.png", "MideaLogo/MideaLogo_13.png",
    "MideaLogo/MideaLogo_15.png", "MideaLogo/MideaLogo_18.png", "MideaLogo/MideaLogo_20.png", "MideaLogo/MideaLogo_23.png", "MideaLogo/MideaLogo_26.png",
    "MideaLogo/MideaLogo_29.png", "MideaLogo/MideaLogo_32.png", "MideaLogo/MideaLogo_38.png", "MideaLogo/MideaLogo_45.png", "MideaLogo/MideaLogo_50.png",
    "MideaLogo/MideaLogo_52.png", "MideaLogo/MideaLogo_53.png", "MideaLogo/MideaLogo_58.png", "MideaLogo/MideaLogo_60.png", "MideaLogo/MideaLogo_64.png",
};

// char* midea_logo_img[] = {
//     "MideaLogo/MideaLogo_00.png", "MideaLogo/MideaLogo_01.png", "MideaLogo/MideaLogo_02.png", "MideaLogo/MideaLogo_03.png", "MideaLogo/MideaLogo_04.png",
//     "MideaLogo/MideaLogo_00.png", "MideaLogo/MideaLogo_06.png", "MideaLogo/MideaLogo_07.png", "MideaLogo/MideaLogo_08.png", "MideaLogo/MideaLogo_09.png",
//     "MideaLogo/MideaLogo_00.png", "MideaLogo/MideaLogo_11.png", "MideaLogo/MideaLogo_12.png", "MideaLogo/MideaLogo_13.png", "MideaLogo/MideaLogo_14.png",
//     "MideaLogo/MideaLogo_15.png", "MideaLogo/MideaLogo_16.png", "MideaLogo/MideaLogo_17.png", "MideaLogo/MideaLogo_18.png", "MideaLogo/MideaLogo_19.png",
//     "MideaLogo/MideaLogo_20.png", "MideaLogo/MideaLogo_21.png", "MideaLogo/MideaLogo_22.png", "MideaLogo/MideaLogo_23.png", "MideaLogo/MideaLogo_24.png",

//     "MideaLogo/MideaLogo_25.png", "MideaLogo/MideaLogo_26.png", "MideaLogo/MideaLogo_27.png", "MideaLogo/MideaLogo_28.png", "MideaLogo/MideaLogo_29.png",
//     "MideaLogo/MideaLogo_30.png", "MideaLogo/MideaLogo_31.png", "MideaLogo/MideaLogo_32.png", "MideaLogo/MideaLogo_33.png", "MideaLogo/MideaLogo_34.png",
//     "MideaLogo/MideaLogo_35.png", "MideaLogo/MideaLogo_36.png", "MideaLogo/MideaLogo_37.png", "MideaLogo/MideaLogo_38.png", "MideaLogo/MideaLogo_39.png",
//     "MideaLogo/MideaLogo_40.png", "MideaLogo/MideaLogo_41.png", "MideaLogo/MideaLogo_42.png", "MideaLogo/MideaLogo_43.png", "MideaLogo/MideaLogo_44.png",
//     "MideaLogo/MideaLogo_45.png", "MideaLogo/MideaLogo_46.png", "MideaLogo/MideaLogo_47.png", "MideaLogo/MideaLogo_48.png", "MideaLogo/MideaLogo_49.png",

//     "MideaLogo/MideaLogo_50.png", "MideaLogo/MideaLogo_51.png", "MideaLogo/MideaLogo_52.png", "MideaLogo/MideaLogo_53.png", "MideaLogo/MideaLogo_54.png",
//     "MideaLogo/MideaLogo_55.png", "MideaLogo/MideaLogo_56.png", "MideaLogo/MideaLogo_57.png", "MideaLogo/MideaLogo_58.png", "MideaLogo/MideaLogo_59.png",
//     "MideaLogo/MideaLogo_60.png", "MideaLogo/MideaLogo_61.png", "MideaLogo/MideaLogo_62.png", "MideaLogo/MideaLogo_63.png", "MideaLogo/MideaLogo_64.png",
// };

ui_startup_widget_t us = {
    .step      = 0,
    .l_cnt     = 0,
    .img_logo  = NULL,
    .logo_time = NULL,
};


int logo_getFileSize(const char* fileName) {
    struct stat st;
    if (stat(fileName, &st) == -1) {
        return 0;  // 返回-1表示获取文件大小失败
    }
    return st.st_size;
}

int startup_logo_res_init(void)
{
    int i = 0;
    char new_path[1024];
    char normal_path[1024];
    lv_img_header_t header;

    int fp = -1;
	unsigned int w, h;
	uint32_t sbuf_len = 0;
	unsigned char* dbuf = NULL;
	unsigned char* sbuf = NULL;

    unsigned char ucdata;
	unsigned char* p = NULL;
	int32_t t, j;
    int ret = -1;

	int32_t total_size = 0;

    for(i=0;i<LOGO_NUM;i++)
    {
        memset(normal_path,0,sizeof(normal_path));
        #ifdef PT_BK7258
        snprintf(normal_path,sizeof(normal_path),"/mnt/lfs/%s",midea_logo_img[i]);
        #else
		snprintf(normal_path,sizeof(normal_path),"./res/%s",midea_logo_img[i]);
        #endif

		memset(new_path,0,sizeof(new_path));
        #ifdef PT_BK7258
        snprintf(new_path,sizeof(new_path),"L:/mnt/lfs/%s",midea_logo_img[i]);
        #else
		snprintf(new_path,sizeof(new_path),"L:./res/%s",midea_logo_img[i]);
        #endif
        debug_d("new_path=%s\n",new_path);

    	ret = lv_img_decoder_get_info(new_path, &header);
        debug_d("ret=%d\n",ret);

        sbuf_len = logo_getFileSize(normal_path);
        if(!sbuf_len){
            continue;
        }

		sbuf = psram_malloc(sbuf_len);
		if(!sbuf){
            debug_d("err\n");
			continue;
		}

		fp = open(normal_path,O_RDWR);
		if(fp < 0){
			free(sbuf);
			sbuf = NULL;
            debug_d("open err\n");
			continue;
		}

		read(fp,sbuf,sbuf_len);
		close(fp);
		fp = -1;

		ret = lv_png_get_argb_from_buff(&dbuf,sbuf,sbuf_len,&w,&h);
		if(ret){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		t = w * h;
		p = dbuf;

        l_d("w:h=%d:%d\n",w,h);

        us.logo_img[i].header.cf  		    = header.cf;
    	us.logo_img[i].header.always_zero   = header.always_zero;
    	us.logo_img[i].header.reserved      = header.reserved;
    	us.logo_img[i].header.w  			= header.w;
    	us.logo_img[i].header.h 			= header.h;
    	us.logo_img[i].data_size			= header.w*header.h*4;
    	us.logo_img[i].data 				= (const uint8_t*)dbuf;

		total_size = total_size + header.w*header.h*4;

		free(sbuf);
		sbuf = NULL;
    }

    l_d("logo_total_size=%dk\n",total_size/1024);

    return 0;
}

int startup_logo_res_deinit(void)
{
    int i = 0;
    for(i=0;i<LOGO_NUM;i++)
    {
        if(us.logo_img[i].data){
            psram_free(us.logo_img[i].data);
            us.logo_img[i].data = NULL;
        }
    }

    return 0;
}

char* startup_loading_img[] = {
    "Loading/Loading_00.png",
    "Loading/Loading_01.png",
    "Loading/Loading_02.png",
    "Loading/Loading_03.png",
    "Loading/Loading_04.png",

    "Loading/Loading_05.png",
    "Loading/Loading_06.png",
    "Loading/Loading_07.png",
    "Loading/Loading_08.png",
    "Loading/Loading_09.png",

    "Loading/Loading_10.png",
    "Loading/Loading_11.png",
    "Loading/Loading_12.png",
    "Loading/Loading_13.png",
    "Loading/Loading_14.png",

    "Loading/Loading_15.png",
    "Loading/Loading_16.png",
    "Loading/Loading_17.png",
};

int startup_loading_res_init(void)
{
    int i = 0;
    char new_path[1024];
    char normal_path[1024];
    lv_img_header_t header;

    int fp = -1;
	unsigned int w, h;
	uint32_t sbuf_len = 0;
	unsigned char* dbuf = NULL;
	unsigned char* sbuf = NULL;

    unsigned char ucdata;
	unsigned char* p = NULL;
	int32_t t, j;
    int ret = -1;

	int32_t total_size = 0;

    for(i=0;i<LOADING_NUM;i++)
    {
        memset(normal_path,0,sizeof(normal_path));
        #ifdef PT_BK7258
        snprintf(normal_path,sizeof(normal_path),"/mnt/lfs/%s",startup_loading_img[i]);
        #else
		snprintf(normal_path,sizeof(normal_path),"./res/%s",startup_loading_img[i]);
        #endif

		memset(new_path,0,sizeof(new_path));
        #ifdef PT_BK7258
        snprintf(new_path,sizeof(new_path),"L:/mnt/lfs/%s",startup_loading_img[i]);
        #else
		snprintf(new_path,sizeof(new_path),"L:./res/%s",startup_loading_img[i]);
        #endif
        debug_d("new_path=%s\n",new_path);

    	ret = lv_img_decoder_get_info(new_path, &header);
        debug_d("ret=%d\n",ret);

        sbuf_len = logo_getFileSize(normal_path);
        debug_d("sbuf_len=%d\n",sbuf_len);
        if(!sbuf_len){
            continue;
        }

		sbuf = psram_malloc(sbuf_len);
		if(!sbuf){
			continue;
		}

		fp = open(normal_path,O_RDWR);
		if(fp < 0){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		read(fp,sbuf,sbuf_len);
		close(fp);
		fp = -1;

		ret = lv_png_get_argb_from_buff(&dbuf,sbuf,sbuf_len,&w,&h);
		if(ret){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		t = w * h;
		p = dbuf;

        l_d("w:h=%d:%d\n",w,h);

        us.loading_img[i].header.cf  		  = header.cf;
    	us.loading_img[i].header.always_zero  = header.always_zero;
    	us.loading_img[i].header.reserved     = header.reserved;
    	us.loading_img[i].header.w  		  = header.w;
    	us.loading_img[i].header.h 			  = header.h;
    	us.loading_img[i].data_size			  = header.w*header.h*4;
    	us.loading_img[i].data 				  = (const uint8_t*)dbuf;

		total_size = total_size + header.w*header.h*4;

		free(sbuf);
		sbuf = NULL;
    }

    l_d("loading_total_size=%dk\n",total_size/1024);

    return 0;
}

int startup_loading_res_deinit(void)
{
    int i = 0;
    for(i=0;i<LOADING_NUM;i++)
    {
        if(us.loading_img[i].data){
            psram_free(us.loading_img[i].data);
            us.loading_img[i].data = NULL;
        }
    }

    return 0;
}

void startup_timer_callback(lv_timer_t* t)
{
    if(us.step == 0)
    {
        us.step  = 1;
        lv_timer_set_period(us.logo_time,30);
    }
    else if(us.step == 1)
    {
        startup_loading_res_init();
        us.step = 2;
    }
    else if(us.step == 2)
    {
        midea_ui_safe_add_flag(us.img_logo,LV_OBJ_FLAG_HIDDEN);

        midea_ui_safe_clear_flag(us.img_load_tip,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_clear_flag(us.img_loading,LV_OBJ_FLAG_HIDDEN);
        midea_ui_set_img_src(us.img_loading,(void*)(&us.loading_img[us.l_cnt]));
        us.step = 3;
    }
    else if(us.step == 3)
    {
        us.l_cnt++;
        if(us.l_cnt >= LOADING_NUM){
            us.l_cnt = 0;
            us.step  = 4;
        }
        midea_ui_set_img_src(us.img_loading,(void*)(&us.loading_img[us.l_cnt]));

        // if(root_get_loading_flag() == 3)
        // {
        //     us.step = 3;
        // }
    }
    else if(us.step == 4)
    {
        if(us.logo_time){
            midea_ui_safe_del_timer(us.logo_time);
            us.logo_time = NULL;
        }

        root_page_enter_home(LOAD_ANIM_NONE);

        // if(pt_accredit_tokenpwd_get())
        // {
        //     if(pt_user_agreement_get()){
        //         debug_d("---enter page:home \n");
        //         com_title_create();
        //         root_page_enter_home(LOAD_ANIM_NONE);
        //     }else{
        //         debug_d("---enter page:user_agreement \n");
        //         page_install("user_agreement",lv_page_user_agreement_create(),NULL);
        //         page_push_anim("user_agreement",LOAD_ANIM_MOVE_LEFT);
        //         first_title_create();
        //     }
        // }else{
        //     debug_d("---enter page:download\n");
        //     page_install("download",lv_page_download_app_create(),NULL);
        //     page_push_anim("download",LOAD_ANIM_MOVE_LEFT);
        //     first_title_create();
        // }
    }
}

int play_startup_animation(lv_obj_t *superview)
{
    debug_d("logo enter\n");
    us.img_logo = lv_img_create(superview);
    lv_obj_set_pos(us.img_logo,0,0);
    midea_ui_set_img_src(us.img_logo,pt_fs_get("logo.png"));

    us.img_load_tip = lv_img_create(superview);
    lv_obj_set_pos(us.img_load_tip,192,200);
    midea_ui_set_img_src(us.img_load_tip,pt_fs_get("loading.png"));
    midea_ui_safe_add_flag(us.img_load_tip,LV_OBJ_FLAG_HIDDEN);

    us.img_loading = lv_img_create(superview);
    lv_obj_set_pos(us.img_loading,196,118);
    midea_ui_safe_add_flag(us.img_loading,LV_OBJ_FLAG_HIDDEN);

    us.logo_time = lv_timer_create(startup_timer_callback,3000,NULL);

    return 0;
}


void ui_init_startup_page_src(lv_obj_t *superview)
{
    // startup_logo_res_init();
    play_startup_animation(superview);
}

void ui_release_startup_page_src(lv_obj_t *superview)
{
    us.step = 0;

    startup_loading_res_deinit();

    if(us.img_logo){
        midea_ui_safe_del_obj(us.img_logo);
        us.img_logo = NULL;
    }
    if(us.img_loading){
        midea_ui_safe_del_obj(us.img_loading);
        us.img_loading = NULL;
    }
    if(us.logo_time){
        midea_ui_safe_del_timer(us.logo_time);
        us.logo_time = NULL;
    }
}

static void startup_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_load(page_base_t* self)
{
    ui_init_startup_page_src(self->root);
    return ;
}
static void startup_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void startup_page_on_view_did_unload(page_base_t* self)
{
    ui_release_startup_page_src(self->root);
    debug_d("logo exit\n");

    return ;
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = startup_page_on_custom_attr_config,
    .on_view_load           = startup_page_on_view_load,
    .on_view_did_load       = startup_page_on_view_did_load,
    .on_view_will_appear    = startup_page_on_view_will_appear,
    .on_view_did_appear     = startup_page_on_view_did_appear,
    .on_view_will_disappear = startup_page_on_view_will_disappear,
    .on_view_did_disappear  = startup_page_on_view_did_disappear,
    .on_view_did_unload     = startup_page_on_view_did_unload,
};


page_vtable_t* lv_page_startup_create(void)
{
    return &page_param;
}